package com.hos.punchtheclocksystem.interceptor;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hos.punchtheclocksystem.pojo.DoctorAccount;
import com.hos.punchtheclocksystem.service.impl.SysUserService;
import com.hos.punchtheclocksystem.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

/**
 * @author Hanyixuan
 */
@Component
public class SessionTimeoutInterceptor implements HandlerInterceptor {

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


//        System.out.println("获取到的getServletContext的docID："+request.getServletContext().getAttribute("docId"));


        HttpSession session = request.getSession();
        // 从session中获取用户信息
        DoctorAccount loginUser = (DoctorAccount) session.getAttribute("loginUser");

        // session过期
        if(loginUser == null){
            // 通过接口跳转登录页面
            response.sendRedirect("/login");
            return false;
        }else{
            return true;
        }
    }

}
